import 'dart:typed_data';

import '../hex/hex_utils.dart';

extension CUStringExtensions on String {
  String? getOrNull(int index) {
    return index >= 0 && length > index ? this[index] : null;
  }

  String getOrEmpty(int index, {String def = ""}) {
    return index >= 0 && length > index ? this[index] : def;
  }

  /// 将String转化成ASCII byte[]
  ///
  /// [limit] 长度 限制
  /// [def] 默认值
  /// [isFixed] 是否需要固定长度，大于[limit]就会裁剪
  Uint8List strToASCII({int? limit, int def = 0x00, bool isFixed = true}) {
    final codeData = codeUnits;
    Uint8List bytes;
    if (limit == null) {
      return byteArrayOf(codeData);
    }
    // 如果长度小于[length]，就会补def
    if (length < limit) {
      bytes = byteArrayOf(codeUnits + List.generate(limit - length, (index) => def));
    } else {
      //是否需要固定长度，大于[length]就会裁剪
      if (isFixed) {
        bytes = byteArrayOf(codeData.sublist(0, limit));
      } else {
        bytes = byteArrayOf(codeData);
      }
    }
    return bytes;
  }

  Uint8List strToASCIIFixedLength(int length) {
    var bytes = byteArrayOf(codeUnits);
    if (bytes.length < length) {
      bytes = Uint8List.fromList(List.generate(length, (index) => index < bytes.length ? bytes[index] : 0x00));
    }
    return bytes;
  }

  /// 将二进制转成int
  int? toInt0B({bool isReversed = false}) {
    try {
      var text = replaceAll(
        "0b",
        "",
      ).replaceAll(
        "0B",
        "",
      );
      if (isReversed) {
        return int.parse(text.split("").reversed.join(), radix: 2);
      }
      return int.parse(text, radix: 2);
    } catch (e) {
      return null;
    }
  }

  int toInt() => int.parse(this);

  double toDouble() => double.parse(this);

  int? toIntOrNull() {
    try {
      return int.parse(this);
    } catch (e) {
      return null;
    }
  }

  int toIntOrEmpty({int def = 0}) {
    try {
      return int.parse(this);
    } catch (e) {
      return def;
    }
  }

  double? toDoubleOrNull() {
    try {
      return double.parse(this);
    } catch (e) {
      return null;
    }
  }

  double toDoubleOrEmpty({double def = 0.0}) {
    try {
      return double.parse(this);
    } catch (e) {
      return def;
    }
  }

  ///忽略大小写比较
  bool equals(String other, [bool ignoreCase = false]) {
    if (ignoreCase) {
      return toLowerCase() == other.toLowerCase();
    } else {
      return this == other;
    }
  }

  ///忽略大小写比较
  bool containsX(String other, [bool ignoreCase = false]) {
    if (ignoreCase) {
      return toLowerCase().contains(other.toLowerCase());
    } else {
      return this.contains(other);
    }
  }

  /// 将字符串按指定长度分割并连接
  String chunkAndJoin(int chunkSize, String separator) {
    if (isEmpty || chunkSize <= 0) return "";

    final Iterable<String> chunks =
        Iterable.generate((length + chunkSize - 1) ~/ chunkSize, (int i) => substring(i * chunkSize, (i * chunkSize + chunkSize) > length ? length : i * chunkSize + chunkSize));
    // 使用给定的分隔符连接各块
    return chunks.join(separator.toString());
  }

  String ifEmpty(String def) {
    return isEmpty ? def : this;
  }

  String ifNoEmpty(Function(String it) def) {
    return isNotEmpty ? def(this) : this;
  }

  bool hasMatch(String pattern) {
    return RegExp(pattern).hasMatch(this);
  }
}

extension CUStringNullExt on String? {
  String orEmpty() {
    return this ?? "";
  }

  bool isNullOrEmpty() {
    return this == null || this!.isEmpty;
  }
}
